<?php

if(!defined('IN_WEB')) {
	exit('Access Denied');
}
include_once (S_ROOT . './source/api_class.php');

$refer = empty($_GET['refer'])?rawurldecode($_SCOOKIE['_refer']):$_GET['refer'];
if(empty($refer)) {
	$refer = 'user.php?ac=index';
}
if($_SGLOBAL['supe_uid']) {
	showmessage('登陆成功', $refer, 0);
}
if(submitcheck('signinsubmit')) {
	$username = trim($_POST['username']);
	$password = $_POST['password'];
	$cookietime = intval($_POST['cookietime']);

	if(empty($username) || empty($password)) {
		showmessage('用户名或者密码不能为空', 'do.php?ac=signin');
	}
	
	$client = new FFapiClient ( null );
	$data= $client->getAccessToken ( $username, md5 ( $password ) );
	
	if($client->error){
		showmessage("登陆失败! error code:$client->error", "do.php?ac=signin", 1);
	}
	$_SESSION['user'] = $data;
	$user = $data['resultData'] ;
	$uid = $user['userInfo']['uid'];
	//设置cookie
	$cookietime = 31536000;
	ssetcookie('auth', authcode("$data[token]\t$uid", 'ENCODE'), $cookietime);
	ssetcookie('loginuser',  $user['userInfo']['account'], 31536000);
	ssetcookie('_refer', '');
	if(empty($_POST['refer'])) {
		$_POST['refer'] = $refer;
	}

	$client = new FFapiClient ( $data['token'] );
	$adminuids = $client->postManage("getAdminList",array(), $uid);
	$_SESSION['adminuids'] = $adminuids;
		
	showmessage('登陆成功', $_POST['refer'], 1);
}

include template('do_signin');

?>